<template>
  <el-container>
    <div style=" margin-top: 100px; display: flex;
        width: 1100px;
        height: 600px;
        background: #f5f5f5;
      ">
      <el-aside width="200px" class="friend-list">
        <div class="search">
          <el-input class="search-input" size="mini" style="height: 10px; width: 150px" v-model="searchFriendUserName"
            placeholder="搜索">
          </el-input>
          <img @click="findFriend" class="search-icon" src="../../public/images/添加好友.png" />
        </div>
        <div :class="currentFriend == '新的朋友' ? 'friend-list-item Active' : 'friend-list-item'"
          @click="currentFriend = '新的朋友'">
          <img class="avatar" src="../../public/images/新的朋友.png" />
          <div class="friend-list-item-info">
            <div class="friend-list-item-name">新的朋友</div>
          </div>
        </div>
        <div v-if="(item.id != user.id && (item.status == 1 || item.status == 5))" v-for="item in friends" :class="
          item == currentFriend ? 'friend-list-item Active' : 'friend-list-item'
        " :key="item.id" @click="chooseFriend(item)">
          <template>
            <img class="avatar" :src="item.avatar" />
            <div class="friend-list-item-info">
              <span class="friend-list-item-name"> {{ item.nickName }}</span>
            </div>
          </template>
        </div>

      </el-aside>
      <el-container>
        <el-main>
          <div v-if="currentFriend.id != null" class="friend-detail">
            <div class="info-wrap">
              <img :src="currentFriend.avatar" />
              <div class="info">
                <div class="info-top">
                  <div class="info-name">
                    {{ currentFriend.nickName }}
                  </div>
                  <div class="info-main-username">
                    蛙聊号：{{ currentFriend.username }}
                    <br>
                    邮箱：{{ currentFriend.email }}
                  </div>
                  <el-dropdown v-if="currentFriend.status == 1 || currentFriend.status == 5"  class="more" @command="handleCommand">
                    <img src="../../public/images/更多.png" />
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item class="dropdown-item" command="删除好友">删除好友</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                  <img class="sex" :src="
                    currentFriend.sex == '男'
                      ? 'https://api.xmj123.top:8888/down/8B6zMhNrl1rD.png'
                      : 'https://api.xmj123.top:8888/down/bCulAqov4qDK.png'
                  " />

                </div>
                <div class="info-main">
                  来源：通过搜索蛙聊号添加
                </div>
              </div>
            </div>

            <el-button class="send-msg" v-if="currentFriend.status == 8 && currentFriend.id != user.id" @click="addFriend">加为好友</el-button>
            <el-button class="send-msg" v-else @click="sendMsg">发消息</el-button>

          </div>
          <div v-else-if="currentFriend == '新的朋友'">
            <div class="title">新的朋友</div>
            <div class="newFriend" v-for="newFriend in friends" :key="newFriend.id">
              <img :src="newFriend.avatar" />
              <div class="newFriend-detail">
                <div class="newFriend-detail-name">
                  {{ newFriend.nickName }}<br><br>
                  <span class="apply">{{ newFriend.applyMsg }}</span>
                </div>
              </div>
              <div v-if="newFriend.status == 4" class="choose">
                <el-button type="primary" size="small " @click="agreeAddFriend(newFriend)">同意</el-button>
                <el-button type="danger" size="small " @click="rejectAddFriend(newFriend)">拒绝</el-button>
              </div>
              <div v-else-if="newFriend.status == 1 || newFriend.status == 5" class="choose">
                已同意
              </div>
              <div v-else-if="newFriend.status == 3" class="choose">
                待对方验证
              </div>
              <div v-else-if="newFriend.status == 6" class="choose">
                对方拒绝添加
              </div>
              <div v-else-if="newFriend.status == 7" class="choose">
                已拒绝
              </div>
            </div>

            
          </div>
          <div v-else class="default">
            <div class="name">FrogChat</div>
            <img src="https://api.xmj123.top:8888/down/mjtnsJQmRtqV.png" />
          </div>
        </el-main>
      </el-container>
    </div>
  </el-container>
</template>

<script>
import {
  getFriendList,
  addFriend,
  agreeAddFriend,
  findFriendByUserName,
  rejectAddFriend,
  deleteFriend,
} from "@/api/friend";
import { getInfo } from "@/api/user";
import { getToken } from "@/utils/auth";
export default {
  name: "Friend",
  data() {
    return {
      searchFriendUserName: "",
      friends: [],
      currentFriend: {},
      user: {},
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      let _this = this;
      console.log("friend的user", this.user);
      let token = getToken();
      if (_this.user == null || _this.user.id == null) {
        getInfo(token).then(res => {
          if (res.code == 200) {
            _this.user = res.data;
            _this.getFriendList();
          }
        });
      } else {
        this.getFriendList();
      }
    },
    addFriend() {
      let _this = this;
      this.$prompt("请输入验证信息（申请理由）", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^.+$/,
        inputErrorMessage: "验证信息不能为空",
      })
        .then(({ value }) => {
          let o = {
            userId: _this.user.id,
            friendId: _this.currentFriend.id,
            applyMsg: value,
          };
          addFriend(o).then(res => {
            if (res.code != 200) {
              return _this.$message({
                type: "error",
                message: res.msg,
              });
            }
            this.$message({
              type: "success",
              message: "好友申请发送成功",
            });
          });
        })
        .catch(() => {
          console.log("取消加好友");
        });
    },
    findFriend() {
      if (this.searchFriendUserName == "") {
        return this.$message({
          type: "warning",
          message: "请输入蛙聊号进行查找",
        });
      }
      let _this = this;
      findFriendByUserName(_this.searchFriendUserName).then(res => {
        if (res.code != 200) {
          _this.$message({
            type: "warning",
            message: res.msg,
          });
        }
        _this.currentFriend = res.data;
      });
    },
    agreeAddFriend(friend) {
      let _this = this;
      this.$confirm("是否同意对方的好友申请", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        agreeAddFriend({
          userId: _this.user.id,
          friendId: friend.id,
        }).then(res => {
          if (res.code != 200) {
            return _this.$message({
              type: "error",
              message: res.msg,
            });
          }
          friend.status = 1;
        });
      });
    },
    rejectAddFriend(friend) {
      let _this = this;
      this.$confirm("是否拒绝对方的好友申请", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        rejectAddFriend({
          userId: _this.user.id,
          friendId: friend.id,
        }).then(res => {
          if (res.code != 200) {
            return _this.$message({
              type: "error",
              message: res.msg,
            });
          }
          friend.status = 7;
        });
      });
    },
    getFriendList() {
      let _this = this;
      getFriendList(_this.user.id).then(res => {
        if (res.code != 200) {
          return _this.$message({
            type: "error",
            message: res.msg,
          });
        }
        console.log(res);
        _this.friends = res.data;
      });
    },
    chooseFriend(u) {
      this.currentFriend = u;
    },
    sendMsg() {
      if (this.currentFriend.status == 5) {
        this.$message({
          type: "info",
          message: "你不是对方的好友",
        });
      }
    },
    handleCommand(a) {
      let _this = this;
      let userId = this.user.id;
      let friendId = this.currentFriend.id;

      this.$confirm(
        "删除" +
          _this.currentFriend.nickName +
          "后，将同时删除与该联系人的聊天记录。",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        }
      )
        .then(() => {
          deleteFriend({ userId, friendId }).then(res => {
            if (res.code != 200) {
              return _this.$message({
                type: "error",
                message: res.msg,
              });
            }
            _this.$message({
              type: "success",
              message: "已删除",
            });
          });
        })
        .catch(() => {
          console.log("取消删除");
        });
    },
  },
};
</script>

<style lang="less" scoped>
* {
  scrollbar-width: none;
}
.title {
  text-align: left;
  font-size: 25px;
  padding: 8px;
  border-bottom: 1px solid rgba(217, 217, 217, 0.8);
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  //   line-height: 200px;
  max-height: 600px;
  overflow-x: hidden;
  overflow-y: scroll;
  height: 600px;
  width: 230px;
  background: rgba(231, 231, 231, 1);
}

.friend-list::-webkit-scrollbar {
  display: none;
}

.friend-list::-webkit-scrollbar {
  height: 10px !important;
  width: 4px;
}
.friend-list::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 5px transparent;
  border-radius: 10px;
}
.friend-list::-webkit-scrollbar-thumb {
  width: 3px;
  height: 10px;
  background: rgba(128, 128, 128, 0.3);
}

.friend-list-item {
  width: 230px;
  height: 60px;
  display: flex;
  position: relative;

  //   border-bottom: solid 1px #333;
  .avatar {
    padding: 5px;
    width: 50px;
    height: 50px;
    border-radius: 10px;
  }

  .friend-list-item-info {
    width: 120px;

    // background: rgba(255, 0, 0, 0.4);
    .friend-list-item-name {
      float: left;
      text-align: left;
      width: 120px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      height: 50px;
      font-size: 18px;
      line-height: 50px;
      font-family: "黑体";
      padding: 5px;
      //   display: block;
      //   margin-left: -50px;
    }

    .friend-list-item-text {
      float: left;
      padding-left: 5px;
      width: 120px;
      overflow: hidden;
      white-space: nowrap;
      text-align: left;
      text-overflow: ellipsis;
      height: 20px;
      color: rgba(5, 8, 9, 0.4);
      padding-bottom: 2px;
      //   background: blue;
      font-size: 12px;
    }
  }
}

.Active {
  background: #cdcdcc;
}

.friend-list-item:hover {
  background: rgba(209, 208, 208, 0.6);
}

.search {
  width: 230px;
  min-height: 50px;
  padding-top: 5px;
  //   background: red;
  //   background: #f7f7f7;
  display: flex;

  .search-input {
    width: 300px;
    height: 20px;
    margin-left: 10px;
    margin-top: 10px;
    // background: blue;
    background: #e2e2e2 !important;
  }

  .search-icon {
    width: 26px;
    // background: red;
    margin-top: 10px;
    margin-left: 8px;
    height: 25px;

    .search-icon img {
      padding-top: 20px;
      padding: 5px;
    }
  }
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.el-main {
  height: 600px;
  width: 700px;
  background: #f5f5f5;
  color: #333;
  text-align: center;
  // line-height: 160px;
  overflow-x: hidden;
  overflow-y: scroll;
  display: inline-block;
  position: relative;
}

.el-main::-webkit-scrollbar {
  display: none;
}

.default {
  margin-top: 100px;
  display: flex;
  background: #f5f5f5;
  width: 1100px;
  max-width: 1200px;
  position: relative;

  img {
    width: 288px;
    height: 288px;
    position: absolute;
    top: 10%;
    left: 30%;
  }
}

.name {
  font-size: 52px;
  font-family: "Lucida Console", "Courier New", monospace;
  margin-top: 188px;
  margin-left: 30px;
}

.info-wrap {
  margin-top: 100px;
  margin-left: 120px;
  width: 400px;
  height: 100px;
  max-width: 400px;
  display: flex;

  img {
    width: 100px;
    height: 100px;
    border-radius: 20px;
  }

  .info {
    display: block;
    width: 300px;
    height: 100px;
    justify-content: space-between;

    .info-top {
      width: 200px;
      height: 100px;
      margin-left: 30px;
      position: relative;

      .info-name {
        text-align: left;
        font-size: 30px;
      }

      .info-main-username {
        width: 249px;
        height: 50px;
        margin-top: 10px;
        color: grey;
        text-align: left;
      }

      img {
        width: 30px;
        height: 30px;
      }

      .more {
        width: 30px;
        height: 30px;
        position: absolute;
        top: 0;
        right: -20px;

        .dropdown-item:hover {
          background: #e2e2e2;
        }
      }

      .more:hover {
        background: #dddddd;
        border-radius: 0;
      }

      .more /deep/ .el-dropdown-item {
        background: #dddddd !important;
      }

      .sex {
        position: absolute;
        top: 0;
        right: 15px;
      }
    }
  }

  .info-main {
    margin-top: 70px;
    color: #161619;
    width: 200px;
    height: 100px;
  }
}

.friend-detail /deep/ .el-button {
  margin-top: 150px;
  color: #ffffff;
  border-radius: 5px;
  width: 100px;
  height: 40px;
  background: #07c160;
}

.el-dropdown-link {
  cursor: pointer;
  color: black;
}

.el-icon-arrow-down {
  font-size: 12px;
}

.newFriend {
  width: 400px;
  height: 60px;
  padding: 18px;
  display: flex;

  img {
    width: 60px;
    height: 60px;
    margin-right: 10px;
  }

  margin-left: 100px;
  position: relative;

  .apply {
    color: grey;
  }

  .choose {
    position: absolute;
    right: 0;
  }
}
</style>
